import * as request from '@/common/request'

export default {

	getList(map = {}) {
		map.storage = map.storage || '';
		return new Promise((resolve, reject) => {
			request.fly.get('/image', map).then(response => {
				resolve(response)
			})
		})
	},

	delete(id) {
		return new Promise((resolve, reject) => {
			request.fly.delete('/image/' + id).then(response => {
				resolve(response)
			})
		})
	},

	async upload(f, data = {}) {
		const response = await request.fly.get("/config/storage");
		const storageType = response.data.name;
		// console.log(storageType);
		switch (storageType) {
			case "local":
				{
					return this.uploadLocal(f, data);
				}
			case "qiniu":
				{
					return this.uploadQiniu(f, data);
				}
		}
	},
	uploadLocal(f, data = {}) {
		const formData = Object.assign({}, data, {
			"storage": "local"
		});
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: request.config.uploadUrl, //仅为示例，非真实的接口地址
				filePath: f,
				name: 'file', //七牛云单一文件上传必须使用token,file作为key
				header: {},
				formData: formData,
				success: (uploadFileRes) => {
					// console.log(uploadFileRes.data);
					let res_data=uploadFileRes.data;
					resolve( JSON.parse(res_data))
				},
				fail: () => {
					resolve(false);
				}
			});
		});
	},
	// uploadLocal2(data) {
	// 	// console.log(data);
	// 	return new Promise((resolve, reject) => {
	// 		data.append("storage", "local");
	// 		request.fly
	// 			.post("/image", data, {
	// 				headers: {
	// 					"Content-Type": "multipart/form-data"
	// 				}
	// 			})
	// 			.then(response => {
	// 				resolve(response);
	// 			})
	// 			.catch(error => {
	// 				reject(error);
	// 			});
	// 	});
	// },

	//直接传七牛云
	uploadQiniu(f, data) {

		return new Promise((resolve, reject) => {
			request.fly.post("/qiniu").then(response => {
				const token = response.data.token;
				const domain = response.data.domain;
				// console.log(token);

				data.append("token", token);
				//七牛云单一文件上传必须使用token,file作为key
				request.fly.post("http://up.qiniu.com", data, {
						baseURL: '', //覆盖实现
						headers: {
							"Content-Type": "multipart/form-data"
						}
					})
					.then(function(res) {
						console.log("res", res);

						let postData = new FormData();
						postData.append("storage", "qiniu");
						postData.append("base_url", domain);
						postData.append("path", '/' + res.key);

						request.fly
							.post("/image", postData, {
								headers: {
									"Content-Type": "multipart/form-data"
								}
							})
							.then(response => {
								resolve({
									errno: 0,
									errmsg: "",
									data: {
										base_url: domain,
										path: '/' + res.key
									}
								});
							})
							.catch(error => {
								reject(error);
							});


						// resolve({
						//   errno: 0,
						//   errmsg: "",
						//   data: {
						//     base_url: domain,
						//     path: '/' + res.data.key
						//   }
						// });
					})
					.catch(function(err) {
						console.log("err", err);
						reject(err);
					});
			});
		});
	}
};
